var paginator = {
	elementList: null,
	strToParameter: null,
	
	init: function(elementList, fatherElement){
		this.elementList = elementList;
		return this;
	},
	
	// setting the parameter, each element of elementList is set to visible if the element's parameter is equal to the data provided by strToParameter 
	setParameter: function(parameter, strToParameter){
		$(this.elementList).each(function(index){
			var value = strToParameter( $( this ).attr("datetime") );
			
			if(value == parameter){
				$( this ).removeClass("hiddenPaginatorElement");
			}
			else{
				$( this ).addClass("hiddenPaginatorElement");
			}
			
		});
		
	}
}

function myIndexOf(value, array){
	//if(value.typeof() != [].typeof() || value.length != 2){return -1;}
	
	for(i=0; i<array.length; i++){
		if(array[i][0] == value[0] && array[i][1] == value[1]){
			return i;
		}
	}
	return -1;
}


$(document).ready(function(){
	var p = paginator.init($(".paginatorElement"));
	
	var strToParameter = function(string){
		var date = new Date(string);
		var month = date.getMonth() + 1;
		var year = date.getFullYear();
		return [year, month]+'';
	}
	
	var parameterToViewString = function( year_month ){
		year = year_month[0]; month = year_month[1];
		return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][month-1] +' '+ year;
	}
	
	var Y = (new Date()).getFullYear();	// current year
	var M = (new Date()).getMonth()+1;	// current month (1 ··· 12)
	
	// parameterList contains pairs of year and month as lists, with all the pair for the previous, current and next year. 
	var parameterList = []; // example of parameterList: [ [2013,1], [2013,2], ... , [2014,7], ... , [2015,11], [2015,12] ];
	
	var parameterListIndex = null;
	for(y=-1, i=0; y<=1; y++){
		for(m=1; m<=12; m++){
			parameterList.push([Y+y, m]);
			if(y+Y == Y && m == M) { parameterListIndex = i; }
			i++;
		}
	}
	
	function updateButtons(){
		if(parameterListIndex == 0){
			$("#prevBut").addClass("hiddenPaginatorElement");
		}else{
			$("#prevBut").removeClass("hiddenPaginatorElement");
		}
		
		if(parameterListIndex == parameterList.length-1){
			$("#nextBut").addClass("hiddenPaginatorElement");
		}else{
			$("#nextBut").removeClass("hiddenPaginatorElement");
		}
	}
	
	$("#paginatorParameterView").html(parameterToViewString(parameterList[parameterListIndex]));
	p.setParameter(parameterList[parameterListIndex]+'' , strToParameter );
	
	
	$("#nextBut").click(function(){
		parameterListIndex++;
		if(parameterListIndex >= parameterList.length ){
			parameterListIndex = parameterList.length - 1
		}
		newParameter = parameterList[parameterListIndex];
		
		$("#paginatorParameterView").html(parameterToViewString(newParameter));
		p.setParameter(newParameter+'' , strToParameter );
		
		updateButtons();
	});
	
	$("#prevBut").click(function(){
		parameterListIndex--;
		if(parameterListIndex < 0 ){
			parameterListIndex = 0
		}
		newParameter = parameterList[parameterListIndex];
		
		$("#paginatorParameterView").html(parameterToViewString(newParameter));
		p.setParameter(newParameter+'' , strToParameter );
		
		updateButtons();
	});
});


